package com.talent.web.controller;

import com.talent.web.config.minio.FileChunkVO;
import com.talent.web.config.minio.TlFileChunkDTO;
import com.talent.web.model.TalentResult;
import com.talent.web.model.vo.TlFileVo;
import com.talent.web.service.IUploadService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;

/**
 * @ClassName：MinioUploadController
 * @Author: hszhou
 * @Date: 2025/3/3 15:09
 * @Description: 必须描述类做什么事情, 实现什么功能
 */
@RestController
@RequestMapping("/uploader")
@RequiredArgsConstructor
public class UploadController {




    private final IUploadService uploadService;

    /**
     * 检查分片
     *
     * @param chunkDTO
     * @return
     */
    @GetMapping("/chunk")
    public TalentResult<FileChunkVO> checkChunkExist(TlFileChunkDTO chunkDTO) {
        return TalentResult.success(uploadService.checkChunkExist(chunkDTO));
    }

    /**
     * 分片上传
     * @param chunkDTO
     * @return
     */
    @PostMapping("/chunk")
    public TalentResult<Void> uploadChunk(TlFileChunkDTO chunkDTO) {
        uploadService.uploadChunk(chunkDTO);
        return TalentResult.success();
    }

    /**
     * 合并分片
     * @param data
     * @return
     */
    @PostMapping("/merge")
    public TalentResult<Boolean> merge(@RequestBody HashMap<String,String> data) {
        return TalentResult.success(uploadService.merge(data.get("identifier")));
    }


    @GetMapping("/preview/{fileId}")
    public TalentResult<TlFileVo> preview(@PathVariable("fileId") String fileId){
        return TalentResult.success(uploadService.preview(fileId));
    }


    @GetMapping("/download/{fileId}")
    public void  download(@PathVariable("fileId") String fileId, HttpServletResponse response){
        uploadService.dowload(fileId,response);
    }
}
